package com.xjlsoft.weather.controller;

import com.xjlsoft.weather.dto.ResultEntity;
import com.xjlsoft.weather.entity.WeatherTask;
import com.xjlsoft.weather.enums.ResultCodeEnum;
import com.xjlsoft.weather.service.WeatherTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/weatherTask")
@Api(tags = "定时任务管理接口")
public class WeatherTaskController {



	@Autowired
	private WeatherTaskService weatherTaskService;


	/**
     * 所有任务列表
     **/
	@ApiOperation(value = "查询任务列表信息", notes = "显示定时任务的信息")
    @RequestMapping(value="/taskList",method=RequestMethod.POST)
    public Object getTaskList() {
		List<WeatherTask> list=weatherTaskService.taskList();
		ResultEntity result = null;
		if (list != null) {
			result = new ResultEntity(ResultCodeEnum.SUCCESS, list, true);
		} else {
			result = new ResultEntity(ResultCodeEnum.FAILURE, null, false);
		}
		return result;
    }
	
	 /**
     * 根据任务key => 启动任务
     **/
	@RequiresRoles("admin")
    @ApiOperation(value = "根据任务key 启动任务", notes = "根据任务key 启动任务")
    @ApiImplicitParam(name="taskKey",value="任务key值",dataType="string", paramType = "query")
    @RequestMapping(value="/startTask",method=RequestMethod.POST)
    public Object start(@RequestParam("taskKey") String taskKey) {
    	boolean flag=weatherTaskService.start(taskKey);
    	ResultEntity result = null;
		if (flag) {
			result = new ResultEntity(ResultCodeEnum.SUCCESS, null, true);
		} else {
			result = new ResultEntity(ResultCodeEnum.FAILURE, null, false);
		}
		return result;
    }
    
    /**
     * 根据任务key => 停止任务
     */
	@RequiresRoles("admin")
    @ApiOperation(value = "根据任务key  停止任务", notes = "根据任务key  停止任务")
    @ApiImplicitParam(name="taskKey",value="任务key值",dataType="string", paramType = "query")
    @RequestMapping(value="/stopTask",method=RequestMethod.POST)
    public Object stop(@RequestParam("taskKey") String taskKey) {
    	boolean flag=weatherTaskService.stop(taskKey);
    	ResultEntity result = null;
		if (flag) {
			result = new ResultEntity(ResultCodeEnum.SUCCESS, null, true);
		} else {
			result = new ResultEntity(ResultCodeEnum.FAILURE, null, false);
		}
		return result;
    }

    /**
     * 根据任务key => 重启任务
     */
	@RequiresRoles("admin") //需要admin角色 才能访问
    @ApiOperation(value = "根据任务key 重启任务", notes = "根据任务key 重启任务")
    @ApiImplicitParam(name="taskKey",value="任务key值",dataType="string", paramType = "query")
    @RequestMapping(value="/restartTask",method=RequestMethod.POST)
    public Object restart(@RequestParam("taskKey") String taskKey) {
    	boolean flag=weatherTaskService.restart(taskKey);
    	ResultEntity result = null;
		if (flag) {
			result = new ResultEntity(ResultCodeEnum.SUCCESS, null, true);
		} else {
			result = new ResultEntity(ResultCodeEnum.FAILURE, null, false);
		}
		return result;
    }
    
    
    /**
     * 返回当前 正在运行的任务
     */
    @ApiOperation(value = "返回当前 正在运行的任务", notes = "返回当前 正在运行的任务")
    @RequestMapping(value="/printlnTask",method=RequestMethod.POST)
    public Object printlnTask() {
        String flag=weatherTaskService.printlnTask();
    	ResultEntity result = null;
		if (! ("".equals(flag))) {
			result = new ResultEntity(ResultCodeEnum.SUCCESS, flag, true);
		} else {
			result = new ResultEntity(ResultCodeEnum.FAILURE, null, false);
		}
		return result;
    }

}
